package states 
{
	import flash.display.Stage;
	import utilities.KeyboardInputManager;
	import flash.display.Sprite;
	import flash.events.Event;
	// Bring in Box2D
	import Box2D.Dynamics.*
	import Box2D.Collision.*
	import Box2D.Collision.Shapes.*
	import Box2D.Dynamics.Joints.*
	import Box2D.Dynamics.Contacts.*
	import Box2D.Common.Math.*
	/**
	 * ...
	 * @author hlukito
	 */
	public class TestLevel implements IState 
	{
		private var world:b2World;
		private var timestep:Number;
		private var iterations:uint;
		private var pixelsPerMeter:Number = 30;
		private var m_stage:Stage;
		
		public function TestLevel() 
		{
			
		}
		
		/* INTERFACE states.IState */
		
		public function initialize(theStage:Stage, keyboardInputManager:KeyboardInputManager):void 
		{
			m_stage = theStage;
			makeWorld();
			makeWalls();
			makeDebugDraw();
		}
		
		public function update():void 
		{
			world.Step(timestep, iterations, iterations);
			world.ClearForces();
			// Render
			world.DrawDebugData();
		}
		
		public function uninitialize():void 
		{
			
		}
		
		public function destroy():void 
		{
			
		}
		
		private function makeWorld():void
		{
			// Define the gravity vector
			var gravity:b2Vec2 = new b2Vec2(0.0, 10.0);
			// Allow bodies to sleep
			var doSleep:Boolean = true;
			// Construct a world object
			world = new b2World(gravity, doSleep);
			world.SetWarmStarting(true);
			timestep = 1.0 / 30.0;
			iterations = 10;
		}
		
		private function makeDebugDraw():void
		{
			// set debug draw
			var debugDraw:b2DebugDraw = new b2DebugDraw();
			var debugSprite:Sprite = new Sprite();
			m_stage.addChild(debugSprite);
			debugDraw.SetSprite(debugSprite);
			debugDraw.SetDrawScale(30.0);
			debugDraw.SetFillAlpha(0.3);
			debugDraw.SetLineThickness(1.0);
			debugDraw.SetFlags(b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit);
			world.SetDebugDraw(debugDraw);
		}
		
		private function makeWalls():void
		{
			// Note: I am assuming a stage size of 640x400;
			// These placements will put a row of boxes around an area that size.
			
			// We reuse the shape and Body Definitions
			// Box2D creates a different body each time we call world.CreateBody(wallBd);
			var wall:b2PolygonShape = new b2PolygonShape;
			var wallBd:b2BodyDef = new b2BodyDef;
			var wallB:b2Body;
			
			// Left
			wallBd.position.Set(-95 / pixelsPerMeter, 400 / pixelsPerMeter / 2);
			wall.SetAsBox(100 / pixelsPerMeter, 400 / pixelsPerMeter / 2);
			wallB = world.CreateBody(wallBd);
			wallB.CreateFixture2(wall);
			// Right
			wallBd.position.Set((640 + 95) / pixelsPerMeter, 400 / pixelsPerMeter / 2);
			wallB = world.CreateBody(wallBd);
			wallB.CreateFixture2(wall);
			// Top
			wallBd.position.Set(640 / pixelsPerMeter / 2, -95 / pixelsPerMeter);
			wall.SetAsBox(680 / pixelsPerMeter / 2, 100 / pixelsPerMeter);
			wallB = world.CreateBody(wallBd);
			wallB.CreateFixture2(wall);
			// Bottom
			wallBd.position.Set(640 / pixelsPerMeter / 2, (400 + 95) / pixelsPerMeter);
			wallB = world.CreateBody(wallBd);
			wallB.CreateFixture2(wall);
		}
	}

}